$(function () {
    var paymentResult_datagrid = $("#paymentResult_datagrid");
    var paymentResult_dialog = $("#paymentResult_dialog");
    paymentResult_datagrid.datagrid({
        url: "/paymentResult/list",
        fit: true,
        columns: [[
            {field: 'x', checkbox: true},
            {field: 'id', title: 'id', width: 100, hidden: true}, //hidden 隐藏显示该列
            {
                field: 'applicant', title: '投保人', width: 100, formatter: function (value, row, index) {
                if(row.latentClient){
                return row.latentClient.name;
                }
            }
            },
            {field: 'number', title: '核保单号', width: 100},
            {field: 'price', title: '保费金额', width: 100},
            {field: 'limityear', title: '保险年限(/年)', width: 100},
            {
                field: 'dictionaryDetail', title: '缴费方式', width: 100, formatter: function (value, row, index) {
                return value.name;
            }
            },

            {field: 'begindate', title: '起保日期', width: 100},
            {field: 'enddate', title: '止保日期', width: 100},
            {
                field: 'agencyInfo', title: '保险机构', width: 100, formatter: function (value, row, index) {
                return value.name;
            }
            },
            {
                field: 'opId', title: '操作人', width: 100, formatter: function (value, row, index) {
                    if(row.employee){

                    return row.employee.username;
                    }
                }
            },
        ]],
        rownumbers: true,
        striped: true,
        toolbar: "#paymentResult_toolbar",
        pagination: true,
        fitColumns: true,
        singleSelect: true
    })


    //高级查询
    $("#query").click(function () {
        var kw = $("#kerwords").textbox("getValue");
        var number = $("#number").textbox("getValue");
        var beginDate = $("#beginDate").textbox("getValue");
        var endDate = $("#endDate").textbox("getValue");
        console.log(kw);
        paymentResult_datagrid.datagrid('load', {
            keywords: kw,
            number: number,
            beginDate: beginDate,
            endDate: endDate
        });
    })

    //初始化对话框
    paymentResult_dialog.dialog({
        width: 430,
        height: 600,
        buttons: "#bb",
        closed: "true"
    })

    var cmdObj = {
        reload: function () {
            paymentResult_datagrid.datagrid("reload");
        },
        edit: function(){
            //在操作之前应该将以前的数据清空
            $("#paymentResult_form").form("clear");
            //从datagrid中获取选中的那一行数据
            var row = paymentResult_datagrid.datagrid("getSelected");
            console.log(row);
            if (!row) {
                $.messager.alert("温馨提示", "请选择要编辑的行");
            } else {
                //打开对话框
                paymentResult_dialog.dialog("setTitle", "查看收费信息");
                paymentResult_dialog.dialog("open");
                //将选中row数据加载到对话框中的form表单中,用于回显
                //form加载数据原理: 根据同名匹配原则
                //将数据加载到form表单中
                row["agencyInfo.name"] = row.agencyInfo.name;
                row["latentClient.name"] = row.latentClient.name;
                row["dictionaryDetail.name"] = row.dictionaryDetail.name;
                $("#productInfo_datagrid").datagrid({
                    url: '/productInfo/selectProductList?id='+row.agencyInfo.id,
                    title: '已购产品列表',
                    width: 300,
                    columns: [[
                        {field: 'name', title: '险种', width: 130},
                        {field: 'insurance', title: '险种金额', width: 130}
                    ]]
                });
                $("#paymentResult_form").form("load", row);//数据回显
            }
        },
        //关闭对话框
        cancel: function () {
            paymentResult_dialog.dialog("close");
        }
    }


    //调用
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })

})











